package com.mt.fpb.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.github.pagehelper.PageHelper;
import com.mt.fpb.common.util.DateUtil;
import com.mt.fpb.mapper.TMineInfoMapper;
import com.mt.fpb.mapper.TVehicleOutImageMapper;
import com.mt.fpb.mapper.TVehicleOutMapper;
import com.mt.fpb.mapper.TWarningMapper;
import com.mt.fpb.model.TMineInfo;
import com.mt.fpb.model.TVehicleOut;
import com.mt.fpb.model.TVehicleOutImage;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.dto.BaseQueryParams;
import com.mt.fpb.model.vo.CommonPage;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

import static com.mt.fpb.common.util.DateUtil.getPastDate;

/**
 * 2021年6月11日20:33:15
 * 矿山地磅智能检测系统接口
 */
@RestController
@RequestMapping("/TVehicleOut")
public class TVehicleOutController {
    @Resource
    private TVehicleOutMapper tVehicleOutMapper;

    @Resource
    private TWarningMapper tWarningMapper;
    @Resource
    private TVehicleOutImageMapper tVehicleOutImageMapper;

    /**
     * 今日统计数据 LT
     * 左上
     * <p>
     * 2021年6月12日15:52:37
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getCountInfo")
    public CommonResult getCountInfo(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        //今日时间
        JSONObject json = new JSONObject();
        //超重记录 参数 告警状态  今日时间 矿山id
        example.createCriteria().
                andEqualTo("warningType", 3).
                andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%").
                andEqualTo("mineId", queryParams.getMineId());
        example.selectProperties("warningId");
        json.put("isDangerous", tWarningMapper.selectCountByExample(example));
        example.clear();
        //盖棚记录 参数 告警状态 今日时间 矿山id
        example.createCriteria().
                andEqualTo("warningType", 4).
                andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%").
                andEqualTo("mineId", queryParams.getMineId());
        example.selectProperties("warningId");
        json.put("coverTent", tWarningMapper.selectCountByExample(example));
        example.clear();
        //未洗轮记录
        example.createCriteria().
                andEqualTo("warningType", 5).
                andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%").
                andEqualTo("mineId", queryParams.getMineId());
        example.selectProperties("warningId");
        json.put("wheel", tWarningMapper.selectCountByExample(example));
        example.clear();
        //货车车次总计 今日时间 矿山id
        example = new Example(TVehicleOut.class);
        example.createCriteria().
                andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%").
                andEqualTo("checkId", queryParams.getMineId());
        example.selectProperties("vehicleOutId");
        json.put("count", tVehicleOutMapper.selectCountByExample(example));
        example.clear();
        //总记录数
        json.put("num", tVehicleOutMapper.selectCount(null));
        return CommonResult.success(json);
    }

    /**
     * 最近7日 LT
     * 中下
     * <p>
     * 2021年6月12日15:52:33
     *
     * @return
     */
    @GetMapping("getRecentlySevenDays")
    public CommonResult getRecentlySevenDays(BaseQueryParams queryParams) {
        //获取最近7日时间
        List<String> list = DateUtil.findDates(DateUtil.convert2Date(getPastDate(6, "yyyy-MM-dd"), "yyyy-MM-dd"), DateUtil.convert2Date(DateUtil.getCurrentDate("yyyy-MM-DD"), "yyyy-MM-DD"));
        JSONObject json = new JSONObject();
        //7日总数集合
        List<Integer> countList = new LinkedList<>();
        //7日超重集合
        List<Integer> isDangerousList = new LinkedList<>();
        //时间集合
        List<String> dateList = new LinkedList<>();
        //遍历一周时间
        for (String str : list) {
            Example example = new Example(TVehicleOut.class);
            example.createCriteria().
                    andLike("insertTime", "%" + str + "%").
                    andEqualTo("checkId", queryParams.getMineId());
            //通车总数 参数 添加时间 矿山id
            example.selectProperties("vehicleOutId");
            Integer count = tVehicleOutMapper.selectCountByExample(example);
            //清除缓存
            example.clear();
            countList.add(count);
            //超重告警 参数 矿山id 告警状态  添加时间
            example = new Example(TWarning.class);
            example.createCriteria().andEqualTo("mineId", queryParams.getMineId()).
                    andEqualTo("warningType", 3).
                    andLike("updateTime", "%" + str + "%");
            example.selectProperties("warningId");
            Integer isDangerous = tWarningMapper.selectCountByExample(example);
            example.clear();
            isDangerousList.add(isDangerous);
            //转换成前端需要的时间格式
            dateList.add(DateUtil.convert2String(DateUtil.convert2Date(str, "yyyy-MM-dd"), "MM/dd"));
        }
        json.put("count", countList);
        json.put("isDangerous", isDangerousList);
        json.put("time", dateList);
        return CommonResult.success(json);
    }

    /**
     * 事件报警前10条记录 LT
     * 左中
     * <p>
     * 2021年6月12日15:52:26
     *
     * @return
     */
    @GetMapping("getCheckList")
    public CommonResult getCheckList(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        //参数 超载  未盖棚
        example.createCriteria().orEqualTo("warningType", 3).
                orEqualTo("warningType", 4).
                andEqualTo("mineId", queryParams.getMineId());
        example.orderBy("insertTime");
        //前10条记录
        PageHelper.startPage(1, 10);
        //遍历查询
        List<TWarning> tWarnings = tWarningMapper.selectByExample(example);
        return CommonResult.success(tWarnings);
    }

    /**
     * 地磅告警 LT
     * 左下
     * <p>
     * 2021年6月12日15:52:22
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getVehicleOutDangerous")
    public CommonResult getVehicleOutDangerous(BaseQueryParams queryParams) {
        //查询前五条
        PageHelper.startPage(1, 5);
        //倒序关联查询
        List<TWarning> map = tVehicleOutMapper.getVehicleOutDangerous(queryParams.getMineId());
        for (TWarning t : map) {
            List<String> tVehicleOutImages = tVehicleOutImageMapper.getBaseImage(t.getWarningSourceCode());
            if (tVehicleOutImages.size() > 0) {
                t.setImageArray(tVehicleOutImages);
            }
        }
        return CommonResult.success(map);
    }

    /**
     * 超载数量 zfg
     * 2021年6月12日
     *
     * @return
     */
    @GetMapping("getOverLoadNum")
    public CommonResult getOverLoadNum(BaseQueryParams queryParams) {
        JSONObject json = new JSONObject();
        // 单天超载
        int todayCZ = tWarningMapper.getTodayNum(queryParams.getMineId());
        // 全部超载
        int totalCZ = tWarningMapper.getTotalNum(queryParams.getMineId());
        // 超载率
        Integer czRate = tWarningMapper.getLoadRate(queryParams.getMineId());
        // 单天未盖棚
        int todayNum = tWarningMapper.getTNoCoverTent(queryParams.getMineId());
        // 全部未盖棚
        int totalNum = tWarningMapper.getQNoCoverTent(queryParams.getMineId());
        // 未盖棚率
        Integer rate = tWarningMapper.getNoCover(queryParams.getMineId());
        json.put("todayNGP", todayNum);
        json.put("totalNGP", totalNum);
        json.put("gpRate", rate);
        json.put("todayCZ", todayCZ);
        json.put("totalCZ", totalCZ);
        json.put("czRate", czRate);
        return CommonResult.success(json);
    }

}
